import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/event/event_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';


/// 敏捷应急-事故事件
class EventPage extends StatelessWidget {
  EventPage({super.key});

  final controller = Get.put(EventController());

  Widget _buildFilter() {
    return Row(
      children: [
        Expanded(
          child: CustomSearchEnter(
            height: 32.w,
            hintText: '请输入名称',
            text: controller.search,
            onCallBack: (value) {
              controller.search = value;
              controller.refreshPage();
            },
          ),
        ),
        AppGaps.hGap8,
        Container(
          width: 36.w,
          height: 32.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
          child: SvgPicture.asset(
            AppImages.icon_search_screen,
            width: 18.w,
            height: 16.w,
          ),
        ).clickOnce(controller.screen),
      ],
    );
  }

  Widget _buildMain() {
    return Expanded(
      child: CustomEasyRefreshList<EmergencyEventListEntity>(
        key: controller.globalKey,
        dataLoad: (pageIndex, pageSize, {String? search}) async {
          return controller.getListData(pageIndex, pageSize);
        },
        itemBuilder: (EmergencyEventListEntity item, int index) {
          return CustomTitleChartPage(
            title: '${item.accidentName}',
            titleRightWidget: _buildCardTopRight('1'),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                CustomTextWidget.getTextRowView(title: '事故类型:', value: '${item.accidentTypeLabel}'),
                AppGaps.vGap12,
                CustomTextWidget.getTextRowView(title: '事故发生时间:', value: '${item.accidentStartDate}'),
                AppGaps.vGap12,
                CustomTextWidget.getTextRowView(title: '事故报警人:', value: '${item.callPoliceName}'),
                AppGaps.vGap12,
                CustomTextWidget.getTextRowView(title: '调度状态:', value: _status(item.schedulingStatus)),
              ],
            ),
          ).clickOnce(() {
            Get.toNamed(AppRoutes.eventDetail, arguments: item);
          });
        },
      ),
    );
  }

  // 调度状态（0未调度，1已调度）
  String _status(String? i) {
    switch (i) {
      case '0':
        return '未调度';
      case '1':
        return '已调度';
      default:
        return i ?? '';
    }
  }

  Widget _buildCardTopRight(String state) {
    String? stateText;
    Color? color;
    switch (state) {
      case '0':
        stateText = '待登记';
        color = const Color(0xFFFC9900);
        break;
      case '1':
        stateText = '已登记';
        color = const Color(0xFF5C6DFF);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color ?? Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        stateText ?? '',
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.allFilterList,
      onConfirm: controller.screenConfirm,
      topSpacing: AppGaps.vGap20,
      isRadio: true,
      isShowTime: true,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          key: controller.popRightScaffoldKey,
          endDrawer: Container(
            width: AppDimens.sw - 80.w,
            height: AppDimens.sh,
            color: Colors.white,
            child: _getFilterWidget(),
          ),
          body: Container(
            alignment: Alignment.topCenter,
            color: AppColors.bg_color_grey,
            padding: EdgeInsets.symmetric(horizontal: AppDimens.dp14,),
            child: Column(
              children: [
                AppGaps.vGap12,
                _buildFilter(),
                AppGaps.vGap10,
                _buildMain(),
              ],
            ),
          ),

        );
      },
    );
  }
}
